import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:sgy_flutter/extensions/get.dart';
import 'package:sgy_flutter/page/dfu/success_view.dart';
import 'package:sgy_flutter/page/dfu/update_view.dart';
import 'package:sgy_flutter/widget/loading_page.dart';
import 'package:sgy_flutter/widget/sgy_scaffold.dart';

import 'charging_view.dart';
import 'logic.dart';

class DfuPage extends StatefulWidget {
  const DfuPage({Key? key}) : super(key: key);

  @override
  State<DfuPage> createState() => _DfuPageState();
}

class _DfuPageState extends State<DfuPage> {
  final logic = Get.put(DfuLogic());

  String wrapper(String? left, String? right) {
    return "${right != null ? 'L:' : ''}${left} ${right != null ? 'R:${right}' : ''}";
  }

  @override
  Widget build(BuildContext context) {
    return Obx(() => BackButtonListener(
        child: PopScope(
          canPop: DfuPageState.update != logic.state.value,
          onPopInvokedWithResult: (pop, _) {
            if (DfuPageState.update == logic.state.value) {
              logic.showToast("正在升级中，请勿退出！".tr);
            } else {
              // Get.back();
            }
          },
          child: SgyScaffold(
            onLeadingTap: () {
              if (DfuPageState.update == logic.state.value) {
                logic.showToast("正在升级中，请勿退出！".tr);
              } else {
                Get.back();
              }
            },
            title: "固件升级".tr,
            child: Obx(
              () => LoadingPage(
                loading: logic.loading.value,
                child: Container(
                  padding: EdgeInsets.only(top: 28, left: 16, right: 16),
                  child: switch (logic.state.value) {
                    DfuPageState.normal => _buildNormal(),
                    DfuPageState.update => UpdateView(),
                    DfuPageState.success => SuccessView(),
                    DfuPageState.needCharging => ChargingView(),
                  },
                ),
              ),
            ),
          ),
        ),
        onBackButtonPressed: () async {
          if (DfuPageState.update == logic.state.value) {
            logic.showToast("正在升级中，请勿退出！".tr);
            return true;
          } else {
            return false;
          }
        }));
  }

  Widget _buildNormal() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text("固件升级".tr),
        Text(
          "${'当前版本'.tr}: ".tr +
              wrapper(logic.dfuModel.value?.deviceInfo.firmwareRevision,
                  logic.dfuModelRight.value?.deviceInfo.firmwareRevision),
          style: TextStyle(color: Colors.black),
        ).marginOnly(top: 4),
        Text(
          logic.dfuModel.value?.updateInfo?.version == null
              ? "暂无新版本".tr
              : "${'最新版本'.tr}: ".tr +
                  wrapper(logic.dfuModel.value?.updateInfo?.version,
                      logic.dfuModelRight.value?.updateInfo?.version),
          style: TextStyle(color: Get.theme.primaryColor),
        ).marginOnly(top: 4),
        Text("更新内容".tr).marginOnly(top: 16, bottom: 4),
        Text(
          "${logic.dfuModel.value?.updateInfo?.updateContentWithLang ?? ''}",
          style: TextStyle(color: Colors.black),
        ),
        Spacer(),
        Container(
          width: double.infinity,
          padding: EdgeInsets.only(bottom: 12),
          child: FilledButton(
              onPressed: () {
                logic.startUpdate();
              },
              child: Text("立即更新".tr)),
        ),
        Container(
          width: double.infinity,
          padding: EdgeInsets.only(bottom: 20),
          child: FilledButton(
              onPressed: () {
                logic.startFallback();
              },
              child: Text("返回上一个版本".tr)),
        ),
      ],
    );
  }

  @override
  void dispose() {
    Get.delete<DfuLogic>();
    super.dispose();
  }
}
